package com.vcredit.bird.service.eduquery;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.bird.vo.eduquery.EduInitResult;
import com.vcredit.bird.vo.eduquery.EduQueryParamBird;
import com.vcredit.bird.vo.eduquery.EduQueryResponse;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.JacksonUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 学信网查询服务接口
 *
 * @author zhangboshi
 *
 */
@Service
public class EduQueryService extends BirdService {

  // 学信网查询初始化
  @Value("${education.init.tokenUrl}")
  private String EduInit_tokenUrl;
  // 学信网查询
  @Value("${education.query.url}")
  private String EduQuery_url;

  /**
   * 学信网查询初始化
   *
   * @return
   * @throws BirdRequestException
   */
  @Log
  public EduInitResult eduQueryInit(String cacheKey) throws BirdRequestException {

    EduInitResult response = null;
    try {
      BirdResponse birdResponse = super.getBirdData(EduInit_tokenUrl);
      response = JacksonUtil
          .Json2Obj(birdResponse.getOriginalResult(), new TypeReference<EduInitResult>() {
          });
      return response;
    } catch (Exception e) {
      throw new BirdRequestException(e.getMessage());
    }
  }

  /**
   * 学信网查询
   *
   * @return
   * @throws BirdRequestException
   */
  @Log
  public EduQueryResponse eduQuery(EduQueryParamBird queryParam) throws BirdRequestException {
    try {
      String paramData = JacksonUtil.Obj2Json(queryParam);
      BirdResponse birdResponse = super.postBirdData(EduQuery_url, paramData);
      EduQueryResponse response = JacksonUtil.Json2Obj(birdResponse.getResult(),
          new TypeReference<EduQueryResponse>() {
          });
      return response;
    } catch (Exception e) {
      throw new BirdRequestException(e.getMessage());
    }
  }
}
